package com.xc.hotelmgr.webapi.modules.dt.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xc.hotelmgr.webapi.modules.dt.entity.DtEntity;
import com.xc.hotelmgr.webapi.modules.dt.service.DtService;
import com.xc.hotelmgr.webapi.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xc.hotelmgr.webapi.modules.dt.entity.StoreEntity;
import com.xc.hotelmgr.webapi.modules.dt.service.StoreService;
import com.xc.hotelmgr.webapi.common.utils.PageUtils;
import com.xc.hotelmgr.webapi.common.utils.R;



/**
 * 分销商店铺
 *
 * @author kemmy
 * @email kemmy@gmail.com
 * @date 2020-09-10 16:44:23
 */
@RestController
@RequestMapping("dt/store")
public class StoreController extends AbstractController {
    @Autowired
    private StoreService storeService;
    @Autowired
    private DtService dtService;


    @RequestMapping(value = "/list/store", method = RequestMethod.GET)
    public R listStore(Map<String, Object> params) {
        List<StoreEntity> storeEntities =  storeService.sqlSelectByDtPf(params);
        return R.ok().put("list", storeEntities);
    }


    /**
     * 查询机构列表
     */
    @RequestMapping("/dt/list")
    @RequiresPermissions("dt:store:list")
    public R listDt(@RequestParam Map<String, Object> params){
        String inSql = "SELECT id FROM dt_dt WHERE FIND_IN_SET(id, getChild("+  getUserDtId()+ "))";
        List<DtEntity> list =  dtService.getBaseMapper().selectList(new QueryWrapper<DtEntity>().inSql("id",inSql));
        return R.ok().put("list", list);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("dt:store:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = storeService.queryPage(params);
        return R.ok().put("page", page);
    }

    @RequestMapping("/storelist")
    public R storeList() {
        Integer dtId = getUserDtId();
        List<StoreEntity> storeEntities = storeService.selectByDt(dtId);
        return R.ok().put("list", storeEntities);
    }



    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("dt:store:info")
    public R info(@PathVariable("id") Integer id){
		StoreEntity store = storeService.queryById(id);
        return R.ok().put("store", store);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("dt:store:save")
    public R save(@RequestBody StoreEntity store){
		storeService.save(store);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("dt:store:update")
    public R update(@RequestBody StoreEntity store){
		storeService.updateById(store);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("dt:store:delete")
    public R delete(@RequestBody Integer[] ids){
		storeService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
